//package com.t2two.myproject.testprod;
//
//import org.apache.commons.collections.CollectionUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//
//import java.io.IOException;
//import java.util.List;
//import java.util.Objects;
//
///**
// * @version 1.0
// * @description: TODO
// * @author: lifei
// * @date: 2023/8/24 10:19
// */
//public class AttendanceEs {
//
//    @ApiModelProperty(value = "数据库表名",hidden = true)
//    public static final String tableName = "db_delay_application";
//
//    @ApiModelProperty(value = "附件列表字段转string",hidden = true)
//    private String attachListEntityString;
//
//    private Logger logger = LoggerFactory.getLogger(this.getClass());
//    @Autowired
//    private ElasticSearchService elasticSearchService;
//    @Autowired
//    private EsUtil esUtil;
//    @Autowired
//    @Qualifier(value = "restClient")
//    private RestHighLevelClient restClient;
//    @Autowired
//    private EsConfiguration esConfiguration;
//
//
//    try {
//        realTimeInsertData2Es(entity);
//    } catch (Exception e) {
//        logger.error("延迟上班-新增保存到es时异常"+e.getMessage());
//    }
//
//    try {
//        realTimeDeleteData2Es(entity);
//    } catch (Exception e) {
//        logger.error("延迟上班-删除es中数据时异常"+e.getMessage());
//    }
//
//    //更新的时候做更新，如果是已经删除，不调该方法
//    if(Objects.nonNull(entity.getIsDelete()) && !entity.getIsDelete()){
//        try {
//            realTimeUpdateData2Es(entity);
//        } catch (Exception e) {
//            logger.error("延迟上班-更新es中数据时异常"+e.getMessage());
//        }
//    }
//
//    public List<DbRuinLeaveApplication> historyData2EsByCurrent(){
//        List<DbRuinLeaveApplication> entityList = mapper.selectByVague(new DbRuinLeaveApplicationParam());
//        if(CollectionUtils.isNotEmpty(entityList)){
//            for (DbRuinLeaveApplication delayApplication:entityList) {
//                convertData2Es(delayApplication);
//            }
//        }
//        return entityList;
//    }
//
//    public DbRuinLeaveApplication convertData2Es(DbRuinLeaveApplication entity){
//        // 状态
//        String billStatusCode = entity.getBillStatusCode();
//        if (null != billStatusCode) {
//            String billStatusName = (String) redisUtil.hget("bill_status", billStatusCode);
//            entity.setBillStatusName(billStatusName);
//        }
//        // 提单人所在部门
//        Integer departmentId = entity.getDepartmentId();
//        if (null != departmentId) {
//            String departmentName = (String) redisUtil.hget("id_label_department", departmentId.toString());
//            entity.setDepartmentName(departmentName);
//        }
//        //用户名
//        if(entity.getUserId()!=null){
//            String submitterUserName = (String) redisUtil.hget("id_label_user", entity.getUserId().toString());
//            entity.setUserName(submitterUserName);
//        }
//        //附件
//        List<DbRuinLeaveApplicationAttach> attaches = attachService.queryAttachByRuinLeaveId(entity.getId());
//        if(CollectionUtils.isNotEmpty(attaches)){
//            entity.setAttachListEntityString(BasicDataUtil.appendListCharacterString(attaches));
//        }
//        //明细
//        List<DbRuinLeaveApplicationDetails> details = ruinLeaveApplicationDetailsMapper.queryByRuinLeaveId(entity.getId());
//        if(CollectionUtils.isNotEmpty(details)){
//            entity.setDetailsListEntityString(BasicDataUtil.appendListCharacterString(details));
//        }
//        return entity;
//    }
//    public void realTimeInsertData2Es(DbRuinLeaveApplication entity) throws IOException {
//        handleEsIndexByEntity();
//        convertData2Es(entity);
//        EsAddUpdateDataParam esAddUpdateDataParam = handleEsParamByEntity(entity);
//        elasticSearchService.addAsyncDataToEsByIndex(esAddUpdateDataParam);
//    }
//
//    public void realTimeDeleteData2Es(DbRuinLeaveApplication entity) throws IOException {
//        handleEsIndexByEntity();
//        elasticSearchService.deleteDataByIndexId(esConfiguration.environmentalParameters,EsIndexNameEnum.Attendance_Index.getIndexNameCode()+DbRuinLeaveApplication.tableName,String.valueOf(entity.getId()));
//    }
//
//    public void realTimeUpdateData2Es(DbRuinLeaveApplication entity) throws IOException {
//        handleEsIndexByEntity();
//        convertData2Es(entity);
//        EsAddUpdateDataParam esAddUpdateDataParam = handleEsParamByEntity(entity);
//        elasticSearchService.updateDataToEsByIndexId(esAddUpdateDataParam);
//    }
//
//    public void handleEsIndexByEntity() throws IOException {
//        if(!elasticSearchService.existsEsIndex(esConfiguration.environmentalParameters,EsIndexNameEnum.Attendance_Index.getIndexNameCode()+DbRuinLeaveApplication.tableName)){
//            esUtil.createIndex(restClient, esConfiguration.environmentalParameters, EsIndexNameEnum.Attendance_Index.getIndexNameCode()+DbRuinLeaveApplication.tableName,DbRuinLeaveApplication.class,null);
//        }
//    }
//
//    public EsAddUpdateDataParam handleEsParamByEntity(DbRuinLeaveApplication entity){
//        EsAddUpdateDataParam esAddUpdateDataParam = new EsAddUpdateDataParam<>();
//        esAddUpdateDataParam.setDatabaseName(EsConfiguration.databaseName);
//        esAddUpdateDataParam.setIndexName(EsIndexNameEnum.Attendance_Index.getIndexNameCode()+DbRuinLeaveApplication.tableName);
//        esAddUpdateDataParam.setTableName(DbRuinLeaveApplication.tableName);
//        esAddUpdateDataParam.setTodoTypeCode(EsMenuDataEnum.Attendance_Ruin.getTodoTypeCode());
//        entity.setResetFields(null);
//        esAddUpdateDataParam.setDataEntity(entity);
//        return esAddUpdateDataParam;
//    }
//}
